﻿Option Explicit On

Imports System.Xml
Imports System.IO
Imports System.Collections.Generic

Public Class DockWeatherG

    Dim dlocation As String = "Thessaloniki"
    Dim showfar As Boolean = False

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        getWeather(dlocation)

    End Sub
    Public Sub getWeather(ByVal location As String)


        Dim doc As New XmlDocument
        ' Bottomlbl.Text = location

        'Κάνουμε load το xml απο το API της Google
        doc.Load("http://www.google.com/ig/api?weather=" & location)

        'Επεξεργασία του Root Node
        Dim fnode As XmlElement = doc.DocumentElement.FirstChild

        'Ελεγχος για αποτυχία εύρεσης τοποθεσίας
        If fnode.FirstChild.Name = "problem_cause" Then
            'Αν δεν υπάρχει η τοποθεσία,εμφάνιση μηνύματος λάθους
            currenttemp.Text = " "
            conditiontitle.Text = "Location not found"
            bottomlbl.Text = " "
            Humidity.Text = " "
            Wind.Text = " "

        End If


        'Επεξεργασία nodes του  Root Node
        For Each cnode As XmlNode In fnode.ChildNodes
            Select Case cnode.Name
                Case "forecast_information"
                    'Το συγκεκριμένο node περιέχει τις πληροφορίες για την τοποθεσία
                    For Each citynode As XmlNode In cnode.ChildNodes
                        Select Case citynode.Name
                            Case "city"
                                bottomlbl.Text = (citynode.Attributes("data").Value)
                            Case "forecast_date"
                                datelbl.Text = (citynode.Attributes("data").Value)
                        End Select
                    Next
                Case "current_conditions"
                    'Το συγκεκριμένο node περιέχει τις πληροφορίες για τον καιρό
                    For Each ccnode As XmlNode In cnode.ChildNodes
                        Select Case ccnode.Name
                            Case "condition"
                                conditiontitle.Text = (ccnode.Attributes("data").Value)
                            Case "temp_c"
                                If showfar = True Then
                                    currenttemp.Text = (((ccnode.Attributes("data").Value * 9) / 5) + 32) & "F"
                                Else
                                    currenttemp.Text = ccnode.Attributes("data").Value & "C"
                                End If
                            Case "humidity"
                                Humidity.Text = ccnode.Attributes("data").Value
                            Case "wind_condition"
                                Wind.Text = ccnode.Attributes("data").Value
                            Case "icon"
                                currenticon.ImageLocation = "http://www.google.com" & ccnode.Attributes("data").Value
                                currenticon.SizeMode = PictureBoxSizeMode.StretchImage
                                currenticon.Size = New Size(127, 112)

                        End Select
                    Next
            End Select
        Next


    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If TextBox1.Text = String.Empty Then
            TextBox1.Text = "Enter a location"
        Else
            Dim requestedlocation As String = TextBox1.Text
            getWeather(requestedlocation)
        End If

    End Sub


    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked = True Then
            showfar = True
        Else
            showfar = False
        End If
    End Sub
End Class

